﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package demo.modules 
{
import flash.events.Event;
import flash.events.IEventDispatcher;
import jp.progression.core.commands.Command;
import jp.progression.events.CommandCatchEvent;
import jp.progression.events.CommandEvent;

import api.amazon.AmazonAPI;
import api.amazon.parameters.APIContentType;
import api.amazon.parameters.APIOperation;
import api.amazon.parameters.APIResponseGroup;
import api.amazon.parameters.APISearchIndex;

public class AmazonSearch 
{
    private static var api:AmazonAPI;
    
    public static function get SubscriptionId():String  { return api.SubscriptionId; }
    public static function get AssociateTag():String    { return api.AssociateTag; }
    public static function get status():String          { return api.status; }
    
    private static var _data:XML;
    public static function get data():XML
    {
        return _data.copy();
    }
    public static function set data(value:XML):void
    {
        _data = value;
    }
    
    public function AmazonSearch() {}
    
    public static function init( SubscriptionId:String, AssociateTag:String="" ):void
    {
        if ( !api ) {
            api = new AmazonAPI( SubscriptionId, AssociateTag );
            api.Version = "2008-03-03";
            api.addEventListener( Event.COMPLETE , loadCompleteHandler );
        }
    }
    
    public static function rankSearch( nodeNo:uint , index:String="Blended", callback:Function=null ):void
    {
        api.Operation = APIOperation.ITEM_SEARCH;
        api.Style = "http://demo.seyself.com/20080404_prog/amazon.xsl";
        api.ResponseGroup = [ APIResponseGroup.MEDIUM ];
        api.SearchIndex = index;
        api.Keywords = [];
        api.BrowseNode = nodeNo;
        api.connect();
        api.addEventListener( "complete" , 
            function(event:Event) {
                callback( event.target.data );
                event.target.removeEventListener( event.type, arguments.callee );
            }
        );
    }
    
    public static function keywordSearch( keywords:String , index:String="Blended", callback:Function=null ):void
    {
        api.Operation = APIOperation.ITEM_SEARCH;
        api.Style = "http://demo.seyself.com/20080404_prog/amazon.xsl";
        api.ResponseGroup = [ APIResponseGroup.MEDIUM ];
        api.Page = 1;
        api.SearchIndex = index;
        api.Keywords = keywords.split(" ");
        api.BrowseNode = 0;
        api.connect();
        api.addEventListener( "complete" , 
            function(event:Event) {
                callback( event.target.data );
                event.target.removeEventListener( event.type, arguments.callee );
            }
        );
    }
    
    public static function idSearch( itemId:String , callback:Function=null ):void
    {
        api.Operation = APIOperation.ITEM_LOOKUP;
        api.Style = "http://demo.seyself.com/20080404_prog/detail.xsl";
        api.ResponseGroup = [ APIResponseGroup.LARGE ];
        api.Page = 1;
        api.ItemId = itemId;
        api.SearchIndex = "";
        api.Keywords = [];
        api.BrowseNode = 0;
        api.connect();
        api.addEventListener( "complete" , 
            function(event:Event) {
                callback( event.target.data );
                event.target.removeEventListener( event.type, arguments.callee );
            }
        );
    }
    
    public static function cancel():void
    {
        api.cancel();
    }
    
    public static function addEventListener(
        type:String, listener:Function, useCapture:Boolean = false, 
        priority:int = 0, useWeakReference:Boolean = false):void
    {
        api.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }
    
    public static function dispatchEvent(event:Event):Boolean
    {
        return api.dispatchEvent(event);
    }
    
    public static function hasEventListener(type:String):Boolean
    {
        return api.hasEventListener(type);
    }
    
    public static function removeEventListener(type:String,
        listener:Function, useCapture:Boolean = false):void
    {
        api.removeEventListener(type, listener, useCapture);
    }
    
    public static function willTrigger(type:String):Boolean
    {
        return api.willTrigger(type);
    }
    
    
    private static function loadCompleteHandler( event:Event ):void
    {
        data = event.target.data;
    }
    
}

}